package com.hoperun.checkcloth.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hoperun.checkcloth.R;
import com.hoperun.checkcloth.entity.XiaciInfo;

import java.util.ArrayList;
import java.util.List;

public class StuffXcslAdapter extends BaseAdapter {

    private final Context mContext;
    private onDeleteClickListener mOnDeleteClickListener;
    private onCounterClickListener mOnCounterClickListener;
    private final List<XiaciInfo> mList;

    public StuffXcslAdapter(Context context, List<XiaciInfo> list) {
        super();
        mContext = context;
        if (list == null) {
            mList = new ArrayList<>();
        } else {
            mList = list;
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) { // 获得当前项的示例
        final XiaciInfo xcxx;
        xcxx = (XiaciInfo) getItem(position);
        View view;
        final ViewHolder viewholder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_xc_list, parent, false);
            viewholder = new ViewHolder(view);
            view.setTag(viewholder);
        } else {
            view = convertView;
            viewholder = (ViewHolder) view.getTag();
        }

        viewholder.xcmcTv.setText(xcxx.getSJXMC());


        viewholder.xcslTv.setText(xcxx.getXCSL());


        viewholder.deleteBtn.setOnClickListener(v -> {
            if (mOnDeleteClickListener != null) {
                mOnDeleteClickListener.onClick(position);
            }
        });

        viewholder.xcslTv.setOnClickListener(v -> {
            if (mOnCounterClickListener != null) {
                mOnCounterClickListener.onClick(position);
            }
        });
        return view;
    }

    public void setOnDeleteClickListener(onDeleteClickListener onDeleteClickListener) {
        mOnDeleteClickListener = onDeleteClickListener;
    }

    public interface onDeleteClickListener {
        public void onClick(int position);
    }

    public void setOnCounterClickListener(onCounterClickListener onCounterClickListener) {
        mOnCounterClickListener = onCounterClickListener;
    }

    public interface onCounterClickListener {
        void onClick(int position);
    }


    static class ViewHolder {
        TextView xcmcTv;
        TextView xcslTv;
        LinearLayout deleteBtn;

        public ViewHolder(View view) {
            xcmcTv = view.findViewById(R.id.xcmcTv);
            xcslTv = view.findViewById(R.id.xcslTv);
            deleteBtn = view.findViewById(R.id.deleteBtn);
        }
    }

}
